$(function() {
	//alert("hi");
  
  ///替换原有组合music
    $("#li").hide();
  var bag=JSON.parse(localStorage.getItem('bag'));
  

  display_bag(bag);

	
function rand_bing(bag){
  var data = new FormData();

 //var bag=JSON.parse(localStorage.getItem('bag'));
  var rand_selected = new Array();
  var flag =0;
  bag.forEach(function(val,i){
    //var rnd=Math.random();
    if (flag<3){
      rand_selected.push(val);
      flag++;
    }

  })

  //console.log("rand_selected:",rand_selected);
 //https://magi.com/search?q=vanilla&u_atoken=6c8cccf0-d668-4ae1-a7cb-5547d1ac9c97&u_asession=01ldcUJMDcQVWDwHCkOE9mu2NlJYPc6qcwe2WQZTn3fQRb-Ul9VE-X5a3LILAUXSkGX0KNBwm7Lovlpxjd_P_q4JsKWYrT3W_NKPr8w6oU7K93DWk2lTACdeBO84asFMCY4ZzV7dUKUqPAEesf_fWyAmBkFo3NEHBv0PZUm6pbxQU&u_asig=05Csugx6DlSNDFD5vQHxm8bk9DE6F4GD_PJKZqp9g-sZ2FwLu1WwSyevztr1RBcpOLfgcYK7PhaRLNV5t3Bnf6I_5NFpDwrvsY_WLc2Ew8DgzMquV8eQDOOV5KDeYfp9SECkYYzInzcQUmb_8jUne93UX1eGnt7hugvrcKGUQlWdf9JS7q8ZD7Xtz2Ly-b0kmuyAKRFSVJkkdwVUnyHAIJzdejp5c4uDWey7Kgf5xlrv4u0jVfAhu5BUxLGXSbcEGxN_l6fIRQiUubAtyPBWfzGO3h9VXwMyh6PgyDIVSG1W-bJpiGLE1Ub4GWoNUZ78bznWtBrWI9OIMXDMZFVpvEDn7ls3xIMPz1raaD4lsGzg4pCUB2KCXJghkvg-JF-SFVmWspDxyAEEo4kbsryBKb9Q&u_aref=1LDdUYpnCjWExlaWLnsBGBue20Q%3D
var search_path ="https://www.bing.com/search?q="
var search_path_magi="https://magi.com/search?q="
  rand_selected.forEach(function(val,i){
    var word_index=val;
    //console.log("word_index: ",word_index);

    data.append( "word_index", word_index );

    var myHeaders = new Headers();
    var path ="/lemmas/stem/"+word_index.toString();

    fetch(path)
.then(function(response) {
    return response.json();
  })
  .then(function (myJson){
  //  console.log("word_index:",word_index);
  //  console.log(myJson.stem);
 search_path=search_path+myJson.stem+" ";
 search_path_magi=search_path_magi+myJson.stem+" ";
 //console.log("search_path: ",search_path); 
 $('#bing_search_text').text(search_path);
// $('#magi_search_text').text(search_path_magi);
 $('#bing_search').attr('href',search_path);
$('#magi_search').attr('href',search_path_magi);
   return myJson.stem;
  })
  });//end for each





    

};

  // $('#rand_bing').bind('click',rand_bing );


  function shuffle_bag(e){
          $("#li").hide();
         var bag=JSON.parse(localStorage.getItem('bag'));
         for (i=0;i<bag.length;i++){
          var selector ="#li"+i.toString()
          $(selector).remove();
         }

        // console.log(JSON.stringify(bag));
         shuffle(bag);
         localStorage.setItem('bag',JSON.stringify(bag));
        // combine_Music();
         display_bag(bag);
         rand_bing(bag);



    };
    $('#shuffle').bind('click',shuffle_bag );







/**
 * Shuffles array in place.
 * @param {Array} a items An array containing the items.
 */
function shuffle(a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        x = a[i];
        a[i] = a[j];
        a[j] = x;
    }
    return a;
}


function display_bag(bag){
  //combine_Music();

  var cloneCount = 0;
  //console.log(JSON.stringify(bag));
  bag.forEach(function(item){
    var path ="learnmode/jq/"+item.toString();
    
    var id_curr= 'li'+ cloneCount.toString();
    $("#li").clone().attr('id',id_curr).show().appendTo("ul")
    $('#'+id_curr+' '+'.wordMusic').attr('id',(cloneCount.toString()));
    //

    //

  fetch(path)       
  .then(function(response) {
    return response.json();
  })
  .then(function (myJson){
        var wiki_search_path='https://en.wanweibaike.com/wiki-'+myJson.word;
        var youglish_search_path='https://youglish.com/pronounce/'+myJson.word+'/english?';
        $('#'+id_curr+' '+'#user_level').text(item.toString())
        var word=myJson.word;
        $('#'+id_curr+' '+'#word').text(myJson.word);
        $('#'+id_curr+' '+'#word_tip').text(myJson.word_tip);
        $('#'+id_curr+' '+'#word_def').text(myJson.word_def); 
        $('#'+id_curr+' '+'#wiki_search').attr('href',wiki_search_path); 
        $('#'+id_curr+' '+'#youglish_search').attr('href',youglish_search_path);

        $('#'+id_curr+' '+'.wordMusic').attr('src',myJson.wordMusic);
       // console.log(myJson.wordMusic);
        $('#'+id_curr+' '+'#image_a').attr('src',myJson.image_a);
       $('#'+id_curr+' '+'#image_b').attr('src',myJson.image_b);
       $('#'+id_curr+' '+'#clipart_c').attr('src',myJson.clipart_c);
       $('#'+id_curr+' '+'#clipart_d').attr('src',myJson.clipart_d);
 
//


//


       return word

} )
  //
            if (cloneCount>0){
            var pri_li = 'li'+ (cloneCount-1).toString();
            
            $('#'+pri_li+' '+'.wordMusic').bind( 'ended',function () {
             $('#'+pri_li+' '+'.wordMusic').attr('loop',"false");
             $('#'+pri_li+' '+'.wordMusic')[0].pause();
       
             //$(".wordMusic")[song_number+1].load();
             $('#'+id_curr+' '+'.wordMusic')[0].play();
             console.log('link:'+pri_li.toString()+" "+id_curr.toString());
           });

          };
 // console.log($('#'+pri_li+' '+'.wordMusic'));

 cloneCount=cloneCount+1;

});//end for each

};//end display bag







});//end main function



